package top.maof.book.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.maof.book.model.Score;
import top.maof.book.model.Shelf;
import top.maof.book.result.Result;
import top.maof.book.service.ForwardService;
import top.maof.book.service.LikesService;
import top.maof.book.service.ScoreService;
import top.maof.book.service.ShelfService;
import top.maof.book.utils.JWTUtils;
import top.maof.book.vo.RecordVo;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    ShelfService shelfService;

    @Autowired
    ForwardService forwardService;

    @Autowired
    LikesService likesService;

    @Autowired
    ScoreService scoreService;


    @GetMapping("/book/{bookId}")
    public Result<RecordVo> record(@PathVariable("bookId") Integer bookId, @RequestHeader("token") String token) {
        RecordVo recordVo = new RecordVo();
        int useId = Integer.parseInt(JWTUtils.parseToken(token, "id"));
        Shelf select = shelfService.select(useId, bookId);
        recordVo.setIsCollection(select != null);
        recordVo.setIsForward(forwardService.isExist(useId, bookId));
        recordVo.setIsLikes(likesService.isExist(useId, bookId));
        Score score = scoreService.findScoreByUserAndBook(useId, bookId);
        if (score != null) {
            recordVo.setIsScore(true);
            recordVo.setScore(score.getScore());
        } else {
            recordVo.setIsScore(false);
        }

        return Result.success(recordVo);
    }
}
